package tailrec

import scala.annotation.tailrec
/**
  *
  * Desc: 尾递归 测试
  * author: maple
  * Date: 2018-01-20 19:13
  *
  */
object TestTailRec {

  def highFuc(f: (Int, Int) => Int): Unit = {
    f(4, 4)
  }


  def fibonacci(n: Int): Long = {
    val n1 = 1
    val n2 = 1

    @tailrec
    def f(n1: Int, n2: Int, remain: Int): Long = {
      remain match {
        case 0 => n1
        case _ => f(n2, n1 + n2, remain - 1)
      }
    }

    f(n1, n2, n)
  }


  def time[T](f: => T): T = {
    val begin = System.nanoTime()

    val x = f

    val end = System.nanoTime()
    println(s"time take  ${
      (end - begin) / 1000
    }  us")
    x
  }

  time(fibonacci(10))


}
